package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.common.TokenHolder;
import com.itheima.constant.TokenConstant;
import com.itheima.entity.dto.employee.AddEmployeeDto;
import com.itheima.entity.dto.employee.EmployeeLoginDto;
import com.itheima.entity.dto.employee.EmployeeUpDto;
import com.itheima.entity.pojo.Employee;
import com.itheima.entity.vo.employee.EmployeeIdVo;
import com.itheima.entity.vo.employee.EmployeeLoginVo;
import com.itheima.entity.vo.employee.EmployeePageVo;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author huangzelnag
 * @create 2022-05-09
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    EmployeeMapper employeeMapper;





    /**
     * 员工登陆
     *
     * @param employeeLoginDto
     * @return
     */
    @Override
    public EmployeeLoginVo getEmployeeLogin(EmployeeLoginDto employeeLoginDto) {
        String username = employeeLoginDto.getUsername();
        //md5加密
         String password = employeeLoginDto.getPassword();
         password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        Employee employee = new Employee();
        employee.setUsername(username);
        employee.setPassword(password);


        //查询数据库进行校验
        Employee employee1 = employeeMapper.selectByEmployee(employee);

        //判断对象是否为空
        if(ObjectUtil.isEmpty(employee1)){
            return null;
        }

        //获取用户id变成token
        String token =System.currentTimeMillis()+employee1.getId();
        token = DigestUtils.md5DigestAsHex(token.getBytes());

        //将token作为redis的键

        String tokenStr = TokenConstant.EMPLOYEE_TOKEN_PREFIX+token;
        //将对象作为redis的值
        String josnStr = JSON.toJSONString(employee1);

        redisTemplate.opsForValue().set(tokenStr,josnStr,24L, TimeUnit.HOURS);

        EmployeeLoginVo employeeLoginVo = new EmployeeLoginVo();
        BeanUtils.copyProperties(employee1,employeeLoginVo);
        employeeLoginVo.setToken(token);

        return employeeLoginVo;
    }


    /**
     * 退出登陆
     * @param token
     */
    @Override
    public void logout(String token) {


        //将token作为redis的键
        String tokenStr = TokenConstant.EMPLOYEE_TOKEN_PREFIX+token;
        redisTemplate.delete(tokenStr);

    }


    /**
     * 分页查询列表
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public EmployeePageVo getEmployeePage(String name, Integer page, Integer pageSize) {
        EmployeePageVo employeePageVo = new EmployeePageVo();
        employeePageVo.setCurrent(page);

        //1.获取列表总数
        if(!StringUtils.isEmpty(name)){
            name = "%"+name+"%";
        }
        Integer total = employeeMapper.selectCount(name);

        //2.根据分页查询列表
        page =(page-1)*pageSize;
        List<Employee> employees = employeeMapper.selectByPage(name, page, pageSize);


        if(ObjectUtil.isEmpty(employees)){
            return null;
        }

        //3.把列表对象转换成前端对象


        employeePageVo.setRecords(employees);
        employeePageVo.setTotal(total);
        employeePageVo.setSize(pageSize);


        return employeePageVo;
    }



    /**
     * 添加员工信息
     *
     * @param addEmployeeDto
     * @return
     */
    @Override
    public Boolean addEmployee(AddEmployeeDto addEmployeeDto) {
        String username = addEmployeeDto.getUsername();

        Integer count = employeeMapper.selectByUsername(username);
        if(count!=0){
            return false;
        }

        Employee employee = new Employee();
        BeanUtils.copyProperties(addEmployeeDto, employee);

        String idStr = IdUtil.getSnowflake().nextIdStr();
        employee.setId(idStr);
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        employee.setCreateUser(TokenHolder.getCurrentId());
        employee.setUpdateUser(TokenHolder.getCurrentId());

        //密码进行md5加密
        String password = employee.getUsername()+"123456";

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        employee.setPassword(password);


        //3.添加用户
        employeeMapper.insert(employee);

        return true;
    }


    /**
     * 根据用户id获取用户信息
     *
     * @param id
     * @return
     */
    @Override
    public EmployeeIdVo getEmployeeById(String id) {

        Employee employee = employeeMapper.selectById(id);


        EmployeeIdVo employeeIdVo = new EmployeeIdVo();

        BeanUtils.copyProperties(employee,employeeIdVo);


        return employeeIdVo;
    }

    /**
     * 修改用户信息
     *
     * @param employeeUpDto
     * @return
     */
    @Override
    public boolean updateEmployee(EmployeeUpDto employeeUpDto) {
        Employee employee = new Employee();

        BeanUtils.copyProperties(employeeUpDto, employee);
        employee.setUpdateTime(new Date());
        employee.setUpdateUser(TokenHolder.getCurrentId());

       return  employeeMapper.update(employee)>0;
    }
}
